﻿@{
    Layout = "~/Views/Shared/_Index.cshtml";
    //
    var relatedFPH = SysConfigReader.Bool("Outpatient_ChargePage_RelatedFPH");
    //
    var withYjj = SysConfigReader.Bool("ChargeWithAdvanceAccount");
}
<style type="text/css">
    .form .formValue {
        padding: 3px 5px;
    }
</style>

<div style="margin:15px 40px;">
    <table>
        <tr>
            <td>
                选择欠费预结记录：
            </td>
            <td>
                <div class="input-group" style="width:200px;">
                    <select id="seleJs" name="seleJs" class="form-control"></select>
                </div>
            </td>
        </tr>
    </table>
    <div class="gridPanel" style="margin:10px 0;">
        <table id="gridJsmxList"></table>
    </div>

    <div class="panel panel-default" style="margin-top:10px;">
        <div>
            <table class="form">
                <tr>
                    <th class="formTitle"><span style="color:red">*</span>收费日期：</th>
                    <td class="formValue">
                        <input id="sfrq" type="text" class="form-control  form-an input-wdatepicker" style="float: left; margin-left: 5px; width:90%" value="@DateTime.Now.ToString("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" />
                    </td>
                    @if (relatedFPH == true)
                    {
                        <th class="formTitle"><span style="color:red">*</span>发票号：</th>
                        <td class="formValue">
                            <label id="txtfph"></label>
                            <div style="position:absolute;top:5px;right:20px;">
                                <input type="button" class="btn btn-default btn-md" style="margin-left:6px;" value="选发票号" onclick="ShowInvoicePanel()" />
                            </div>
                        </td>
                    }
                    else
                    {
                        <th class="formTitle"></th>
                        <td class="formValue"></td>
                    }
                </tr>
                <tr>
                    <th class="formTitle">折前应收款：</th>
                    <td class="formValue">
                        ￥<label id="zqysk"></label>
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">折扣比例：</th>
                    <td class="formValue">
                        <input id="zkbl" type="text" class="form-control  form-an" style="width:80%;float:left;margin-left: 5px;" /><span style="line-height:25px; margin-left:10px">%</span>
                    </td>
                    <th class="formTitle">折扣金额：</th>
                    <td class="formValue">
                        <input id="zkje" type="text" class="form-control  form-an" style="width:90%" />
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">折后应收款：</th>
                    <td class="formValue">
                        ￥<label id="zhysk"></label>
                    </td>
                </tr>
                <tr id="trYjjzf" style="display:none;">
                    <th class="formTitle">预交金余额：</th>
                    <td class="formValue">
                        ￥<label id="yjjye"></label>
                    </td>
                    <td class="formValue">
                        <div class="ckbox">
                            <input id="ckyjjzfyeqt" type="checkbox"><label for="ckyjjzfyeqt">余额全退</label>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">支付方式：</th>
                    <td class="formValue">
                        <select id="zffs1" class="form-control  form-an" style="width:90%">
                            <option value="">==请选择==</option>
                        </select>
                    </td>
                    <th class="formTitle">收款：</th>
                    <td class="formValue">
                        <input type="text" id="zfje1" class="form-control form-an" style="width:90%" />
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">支付方式：</th>
                    <td class="formValue">
                        <select id="zffs2" class="form-control form-an" style="width:90%">
                            <option value="">==可选择==</option>
                        </select>
                    </td>
                    <th class="formTitle">收款：</th>
                    <td class="formValue">
                        <input type="text" id="zfje2" class="form-control form-an" style="width:90%" />
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">合计实收款：</th>
                    <td class="formValue">￥<label id="hjssk">0.00</label></td>
                </tr>
                <tr>
                    <th class="formTitle">找零：</th>
                    <td class="formValue">￥<label id="zhaoling">0.00</label></td>
                </tr>
            </table>
        </div>
    </div>
</div>
<script type="text/javascript">
    var mzh = $.request('mzh');

    //预交金余额
    var yjjye = 0;
    var withYjj = '@(withYjj)';
    //结算总金额
    var zje = 0;
    //支付应收 结算时应收（zje = 可报 + 结算时应收 + 等等）
    var orglxjzfys = 0;
    //有折扣时不等 original
    var xjzfys = orglxjzfys;    //有折扣时会不等

    $(function () {
        if (withYjj == 'True') {
            $.najax({
                url: "/PatientManage/AccountManage/GetZhyeByPatid?patid=" + $.request("patid"),
                cache: false,
                async: false,
                alertbierror: false,
                success: function (zhyeData) {
                    if (zhyeData && zhyeData.data) {
                        yjjye = zhyeData.data;
                    }
                }
            });
        }

        if (yjjye) {
            $('#trYjjzf').show();
            $('#yjjye').html(parseFloat(yjjye).toFixed(2));
        }

        //现金支付方式1
        $("#zffs1").newtouchBindSelect({
            datasource: function () {
                var resultObjArr = new Array();
                $.each(top.clients.SysForCashPayList, function (key, value) {
                    if (!yjjye && value.xjzffs == '3') {
                        return; //当前账户无预交金余额，则不显示该支付方式
                    }
                    var sele = false;
                    if (!!yjjye && yjjye >= xjzfys) {
                        if (value.xjzffs == '3') {
                            sele = true;
                        }
                    }
                    else if (value.xjzffsmc == "现金") {
                        sele = true;
                    }
                    if (sele) {
                        $('#zffs1').append('<option value="' + value.xjzffs + '" selected=true>' + value.xjzffsmc + '</option>');
                    }
                    else {
                        $('#zffs1').append('<option value="' + value.xjzffs + '">' + value.xjzffsmc + '</option>');
                    }
                });
                return resultObjArr;
            }
        });

        //现金支付方式2
        $("#zffs2").newtouchBindSelect({
            datasource: function () {
                var resultObjArr = new Array();
                $.each(top.clients.SysForCashPayList, function (key, value) {
                    if (value.xjzffs == '3') {
                        return; //不显示该支付方式
                    }
                    $('#zffs2').append('<option value="' + value.xjzffs + '">' + value.xjzffsmc + '</option>');
                });
                return resultObjArr;
            }
        });

        $("#ckyjjzfyeqt").click(function () {
            if ($('#ckyjjzfyeqt').is(':checked')) {
                //预交金支付方式，‘余额全退’
                if (!!yjjye && yjjye >= xjzfys) {
                    //
                    $("#zffs1").val('3').trigger('change');
                    $('#zfje1').val(yjjye.toFixed(2));
                    //
                    $('#zfje2').val('').trigger('change');
                    //
                    hjsskZhaolingCal();
                }
            }
        });

        initFPH();

        $("#seleJs").bindSelect({
            url: "/OutpatientManage/OutpatCharge/GetOutpatientSettArrearageJson?mzh=" + mzh,
            id: "jsnm",
            text: "Text",
        });

        $("#seleJs").trigger('change');
    });

    $("#gridJsmxList").newtouchLocalDataGrid({
        height: 85,
        unwritten: false,
        colModel: [
            { label: 'jsmxnm', name: 'jsmxnm', hidden: true },
            { label: '名称', name: 'mc', width: 160, align: 'left' },
            { label: '数量', name: 'sl', width: 70, align: 'left' },
            {
                label: '金额', name: 'je', width: 100, align: 'left',
                formatter: function (cellvalue) {
                    if (!!cellvalue) {
                        return cellvalue.toFixed(4);
                    } else {
                        return '';
                    }
                }
            },
        ]
    });

    var jsnm;
    $("#seleJs").change(function () {
        reset();
        jsnm = $("#seleJs").val();
        var jsText = $("#seleJs option:selected").html();
        var jeText = jsText.substring(12);
        zje = xjzfys = orglxjzfys = parseFloat(jeText);
        if (!xjzfys) {
            return;
        }
        $('#zqysk').html(xjzfys.toFixed(2));
        $('#zhysk').html(xjzfys.toFixed(2));
        $('#zfje1').val(xjzfys.toFixed(2));
        $('#hjssk').html(xjzfys.toFixed(2));
        $('#zhaoling').html('0.00');
        if (!!yjjye && yjjye >= xjzfys) {
            //预交金
            $('#zffs1').val('3').trigger('change');
        }
        else {
            //现金
            $('#zffs1').val('0').trigger('change');
        }
        //
        $.najax({
            url: "/OutpatientManage/OutpatCharge/GetOutpatientSettDetailGridJson",
            data: { jsnm: jsnm },
            success: function (ajaxResp) {
                $("#gridJsmxList").clearGridData();
                $("#gridJsmxList").newtouchLocalDataGrid(null, ajaxResp);
            }
        });
    });

    //金额相关
    function reset() {
        $('#zkbl').val('');
        $('#zkje').val('');
        $('#zqysk').html('');
        $('#zhysk').html('');
        if ($('#ckyjjzfyeqt').is(':checked')) {
            $('#ckyjjzfyeqt').trigger('click');
        }
        $('#zffs1').val('').trigger('change');
        $('#zfje1').val('');
        $('#zffs2').val('').trigger('change');
        $('#zfje2').val('');
        $('#hjssk').html('');
        $('#zhaoling').html('0.00');
    }

    //提交
    function AcceptClick(callback) {
        var feeRelated = getPostData();
        if (feeRelated) {
            var updateDto = {};
            updateDto.jsnm = jsnm;
            updateDto.sfrq = feeRelated.sfrq;
            updateDto.fph = feeRelated.fph;
            //提交到后台
            $.submitForm({
                url: "@Url.Action("SubmitArrearageSettedUpdate")",
                param: { updateDto: updateDto, feeRelated: feeRelated },
                success: function (ajaxresp) {
                    if (callback) {
                        callback();
                    }
                }
            });
        }
    }

    function getPostData() {
        var sfrq = $('#sfrq').val();
        var fph = $('#txtfph').html();
        var zffs1 = $('#zffs1').val();
        !(!!zffs1 || zffs1 == '0') ? $('#zfje1').val('') : undefined;
        var zfje1 = $('#zfje1').val();
        zfje1 = (!!zffs1 || zffs1 == '0') && !!parseFloat(zfje1) ? parseFloat(zfje1) : 0;
        if (zffs1 == '3' && yjjye) {
            if (!(zfje1 == yjjye || zfje1 <= xjzfys)) {
                $.modalAlert("预交账户支付金额异常", "error");
                return false;
            }
        }
        //
        var zffs2 = $('#zffs2').val();
        !(!!zffs2 || zffs2 == '0') ? $('#zfje2').val('') : undefined;
        var zfje2 = $('#zfje2').val();
        zfje2 = (!!zffs2 || zffs2 == '0') && !!parseFloat(zfje2) ? parseFloat(zfje2) : 0;
        //
        var zkje = $('#zkje').val();
        zkje = !!parseFloat(zkje) ? parseFloat(zkje) : 0;
        var zkbl = $('#zkbl').val();
        zkbl = !!parseInt(zkbl) ? (parseInt(zkbl) / 100).toFixed(2) : 0;
        var hjssk = $('#hjssk').html();
        hjssk = !!parseFloat(hjssk) ? parseFloat(hjssk) : 0;
        var zhaoling = $('#zhaoling').html();
        zhaoling = !!parseFloat(zhaoling) ? parseFloat(zhaoling) : 0;
        //
        if (!!!sfrq) {
            $.modalAlert('缺少收费日期', 'warning');
            return false;
        }
        if ($.getDate() < sfrq) {
            $.modalAlert('收费日期错误', 'warning');
            return false;
        }
        //
        if ($('#txtfph').length == 0) {
            fph = null;
        }
        else {
            if (!fph) {
                $.modalAlert('请选择发票号', 'warning');
                return false;
            }
        }
        //
        if (hjssk != zfje1 + zfje2) {
            $.modalAlert("收款金额异常", "error");
            return false;
        }
        if (hjssk != xjzfys + zhaoling || zhaoling < 0) {
            $.modalAlert("找零金额异常", "error");
            return false;
        }
        return {
            sfrq: sfrq, fph: fph
            , zffs1: zffs1, zfje1: zfje1, zffs2: zffs2, zfje2: zfje2
            , zje: zje, orglxjzfys: orglxjzfys, zkje: zkje, zkbl: zkbl, xjzfys: xjzfys
            , ssk: hjssk, zhaoling: zhaoling
        };
    }

    //common start

    //页面进来，加载发票号
    function initFPH() {
        if (!($("#txtfph").length == 1)) {
            return;
        }
        $.najax({
            url: "/OutpatientManage/OutpatientReg/GetInvoice?r=" + Math.random(),
            dataType: "text",
            cache: false,
            success: function (data) {
                $("#txtfph").html(data);
            }
        });
    }

    //弹出发票号的窗口
    function ShowInvoicePanel() {
        $.modalOpen({
            id: "InvoiceNoPanel",
            title: "选发票号",
            url: "/OutpatientManage/OutpatientReg/ChooseInvoice?from=ConfirmFeeForm",
            width: "300px",
            height: "200px",
            callBack: function (iframeId) {
                top.frames[iframeId].checkFPH();//窗口点确定的回调函数
            }
        });
    }

    //
    $('#zfje1,#zfje2').keyup(function () {
        hjsskZhaolingCal();
        if ($('#ckyjjzfyeqt').is(':checked')) {
            $('#ckyjjzfyeqt').trigger('click');
        }
    });
    $('#zfje1,#zfje2').change(function () {
        hjsskZhaolingCal();
        if ($('#ckyjjzfyeqt').is(':checked')) {
            $('#ckyjjzfyeqt').trigger('click');
        }
    });

    //计算 根据 支付应收、支付方式 计算合计实收款、找零
    function hjsskZhaolingCal() {
        if (!$('#zffs1').val() && $('#zfje1').val() != 0) {
            //未选择支付方式，对应金额要置为0.00
            $('#zfje1').val('0.00');
        }
        var zfje1 = $('#zfje1').val();
        if (!zfje1) {
            zfje1 = '0.00';
        }
        if (!$('#zffs2').val() && $('#zfje2').val() != 0) {
            //未选择支付方式，对应金额要置为0.00
            $('#zfje2').val('0.00');
        }
        var zfje2 = $('#zfje2').val();
        if (!zfje2) {
            zfje2 = '0.00';
        }
        var hjssk = parseFloat(zfje1) + parseFloat(zfje2);  //多种支付方式合计实收
        $('#hjssk').html(hjssk.toFixed(2));
        $('#zhaoling').html(parseFloat(hjssk - xjzfys).toFixed(2));
    }

    //如果有折扣功能start

    //
    $('#zkje,#zkbl').keyup(function () {
        discountCal();
    });

    //折扣比例，折扣金额 计算折扣
    function discountCal() {
        var zkje = $('#zkje').val();
        var zkbl = $('#zkbl').val();
        var je = calZkhMoney(orglxjzfys.toFixed(2), zkje, zkbl);
        if (je === false) {
            //折扣计算失败 只能重置折扣
            $('#zkje').val('');
            $('#zkbl').val('');
            xjzfys = orglxjzfys;
        }
        else {
            xjzfys = je;
        }
        //
        $('#zhysk').html(xjzfys);
        $('#zfje1').val(xjzfys);
        //
        $('#zfje2').val('').trigger('change');
        //
        if ($('#ckyjjzfyeqt').is(':checked')) {
            $('#ckyjjzfyeqt').trigger('click');
        }
        //
        hjsskZhaolingCal();
    }

    //根据折扣比例和折扣金额计算 折后金额
    function calZkhMoney(je, zkje, zkbl) {
        if (je == 0) {
            return 0;
        }
        if (validMoney(je, zkje, zkbl)) {
            if (zkbl) {
                //折扣比例
                var decimalbl = ((100 - parseInt(zkbl)) / 100).toFixed(2);
                je = roundingBy4she6ru5chengshuang(parseFloat(parseFloat(je) * decimalbl), 2);
            }
            if (zkje) {
                if (parseFloat(zkje) > parseFloat(je)) {
                    $.modalAlert('折扣异常，产生负应收', 'warning');
                    return false;
                }
                //折扣金额
                je = roundingBy4she6ru5chengshuang(parseFloat((parseFloat(je) - parseFloat(zkje))), 2);
            }
            return parseFloat(je);
        }
        else {
            return false;
        }
    }

    //验证金额格式是否正确
    function validMoney(zqje, zkje, zkbl) {
        if (zqje == '' || zqje == '0.00') {
            $.modalAlert('费用无效', 'warning');
            return false;
        }
        //折扣金额
        var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
        if (!!zkje) {
            if (!reg.test(zkje) || zkje == '') {
                $.modalAlert('折扣金额格式有误', 'warning');
                return false;
            }
        }

        if (parseFloat(zkje) > parseFloat(zqje)) {
            $.modalAlert('折扣金额不能大于折前金额', 'warning');
            return false;
        }

        //折扣比例
        var blreg = /(^[1-9][0-9]$)|(^100&)|(^[1-9]$)$/;
        if (!!zkbl) {
            if (!reg.test(zkbl)) {
                $.modalAlert('折扣比例格式有误', 'warning');
                return false;
            }
        }
        if (parseInt(zkbl) > 100) {
            $.modalAlert('折扣比例不能大于100', 'warning');
            return false;
        }
        return true;
    }

    //如果有折扣功能end

    //common end

</script>
